import { recoverMember, recoverDetail} from '../../api/member.js';
import {orderPay, createOrder} from "../../api/order";
import Util from '../../utils/util.js';

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.hideHomeButton()
    recoverDetail({}).then(res=>{
      this.setData({
        detail:res.data
      })
    })

    this.onLoadTabbar();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  recoverMember(e){
    let is_need_pay = this.data.detail.is_need_pay
    let price = this.data.detail.price
    let expire_time = this.data.detail.expire_time
    if(is_need_pay == 1){
      Util.showModelTip("在"+expire_time+"前恢复账户需要支付"+price+"元。","账户恢复","去支付")
      .then( res => {
        if(res.confirm){
          this.createOrder();
        }
      })
      return
    }

    Util.showModelTip("恢复账户后，你的主页将恢复访问！","账户恢复")
    .then( res => {
      if(res.confirm){
        recoverMember({ cancel: 1 })
          .then( res => {
            let userInfo = app.globalData.userInfo || {}
            userInfo.cancel = 1
            wx.setStorageSync('USERINFO', JSON.stringify(userInfo))
            app.globalData.userInfo.cancel = 1;

            return app.Tips({
              title: res.msg,
              icon: "success"
            }, {
              tab: 1,
              url: "/pages/member/index"
            })

          }).catch( errmsg => {
            return app.Tips({ title: errmsg })
          })
      }
    })
  },
  onLoadTabbar: function(){//底部tabbar返回数据
    const that = this
    Util.checkIphone().then( res => {//检查是否是iphone x/11等
      wx.createSelectorQuery().in(that).select('.weui-footer')
        .boundingClientRect(function (rect) {
          that.setData({
            bottom: parseInt(rect.height) + 15
          })
        }).exec()

      that.setData({ isIphoneX: res })
    });
  },

  
  createOrder(e) {
    const that = this;

    wx.showLoading({
      title: "去支付..."
    })
    createOrder({
      type: "recover",
      product_id: this.data.detail.record.id,
    }).then(result => {
      const out_trade_no = result.data.out_trade_no
      if (out_trade_no) {
        orderPay({order_id: out_trade_no})
          .then(res => {
            let data = res.data
            wx.requestPayment({
              'timeStamp': data.timeStamp,
              'nonceStr': data.nonceStr,
              'package': data.package,
              'signType': 'MD5',
              'paySign': data.paySign,
              success: function (res) {
                wx.hideLoading()
                let userInfo = app.globalData.userInfo || {}
                userInfo.cancel = 1
                wx.setStorageSync('USERINFO', JSON.stringify(userInfo))
                app.globalData.userInfo.cancel = 1;
                return app.Tips({
                  title: '支付成功',
                  icon: "success"
                }, {
                  tab: 4,
                  url: "/pages/elect-pool/my"
                })
              },
              fail: function (res) {
                wx.hideLoading()
                app.Tips({title: "取消成功", icon: "success"})
              }
            })
          }).catch(errmsg => {
          wx.hideLoading()
          app.Tips({title: errmsg})
        })
      }
    }).catch(errmsg => {
      wx.hideLoading()
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})